package org.jeecg.modules.workflow.convert.change.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.ProtocolChangeApply;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolCreateReqVo;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolRespVo;
import org.jeecg.modules.workflow.convert.change.BpmChgProtocolConvert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : Wzz
 * @create : 2023-04- 27- 9:57
 * @desc :
 */
@Service
public class BpmChgProtocolConvertImpl implements BpmChgProtocolConvert {

    @Autowired
    private IProService proService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public ProtocolChangeApply convert(BpmChgProtocolCreateReqVo bean) {
        if (bean == null) {
            return null;
        }
        ProtocolChangeApply protocolChangeApply = new ProtocolChangeApply();
        BeanUtils.copyProperties(bean, protocolChangeApply);
        protocolChangeApply.setApplyDate(new Date());

        return protocolChangeApply;
    }

    @Override
    public BpmChgProtocolRespVo convert(ProtocolChangeApply bean) {
        if (bean == null) {
            return null;
        }
        BpmChgProtocolRespVo bpmChgProtocolRespVo = new BpmChgProtocolRespVo();

        BeanUtils.copyProperties(bean, bpmChgProtocolRespVo);
        bpmChgProtocolRespVo.setCreateTime(bean.getApplyDate());

        return bpmChgProtocolRespVo;
    }

    @Override
    public BpmChgProtocolRespVo getInfoByProtocolNo(String bean) {

        BpmChgProtocolRespVo bpmChgProtocolRespVo = new BpmChgProtocolRespVo();

        List<Pro> list = proService.list(new LambdaQueryWrapper<Pro>()
                .eq(Pro::getCaProtocolNo, bean)
        );
        if (list.size()==0){
            return null;
        }
        // pro文件同 协议号 的数据，除了 关联账号 其余均相同，因此可直接使用第一个
        Pro pro = list.get(0);

        BeanUtils.copyProperties(pro,bpmChgProtocolRespVo);
        // 重置Id
        bpmChgProtocolRespVo.setId(null);
        // 签约机构名称
        bpmChgProtocolRespVo.setSignedOrgName(
                sysBaseAPI.selectAllById(pro.getSignedOrg()).getDepartName()
        );
        bpmChgProtocolRespVo.setCaContactNm(pro.getLinkerName());
        bpmChgProtocolRespVo.setCaContactPhn(pro.getLinkerMobile());
        bpmChgProtocolRespVo.setCaContactTel(pro.getLinkerTel());
        bpmChgProtocolRespVo.setPostAddress(pro.getAddr());

        return bpmChgProtocolRespVo;
    }

    @Override
    public List<BpmChgProtocolRespVo> convertList(List<ProtocolChangeApply> list) {
        if (list == null) {
            return null;
        }

        List<BpmChgProtocolRespVo> list1 = new ArrayList<BpmChgProtocolRespVo>(list.size());

        for (ProtocolChangeApply protocolChangeApply : list) {
            list1.add(convert(protocolChangeApply));
        }

        return list1;
    }

    @Override
    public IPage<BpmChgProtocolRespVo> convertPage(IPage<ProtocolChangeApply> page) {
        if ( page == null ) {
            return null;
        }

        IPage<BpmChgProtocolRespVo> pageResult = new Page<>();

        pageResult.setRecords( convertList( page.getRecords() ) );
        pageResult.setTotal( page.getTotal() );

        return pageResult;
    }
}
